package com.reiuy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.reiuy.entity.Admin;
import com.reiuy.entity.Student;
import com.reiuy.service.*;
import com.reiuy.vo.ClassInfo;
import com.reiuy.vo.StudentInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.MD5utils;


import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private StudentService studentservice;
    @Resource
    private AdminService adminService;

    @Resource
    private ClassService classService;

    @Resource
    private CourseService courseService;


    @Resource
    private TeacherService teacherService;

    //注册学生
    @RequestMapping("/registerStudent.do")
    @ResponseBody
    public JSONObject registerStudent(@RequestBody Student student){

        //加密密码后重新放入
        student.setStudentpass(MD5utils.getMd5(student.getStudentpass(), "UTF-8", false, 32));

        JSONObject jsonObject = new JSONObject();

        int nums = studentservice.addStudent(student);

        if (nums > 0){
            //返回值大于0 说明注册成功
            jsonObject.put("isSuccess",true);
            jsonObject.put("msg","学生"+student.getStudentname()+"注册成功!");
        }else{
            jsonObject.put("isSuccess",false);
            jsonObject.put("msg","注册失败!");
        }
        return jsonObject;
    }



    //处理登录请求
    @RequestMapping("/loginStudent.do")
    @ResponseBody

    //通过post传过来的是一个json对象，利用@RequestBody注解进行解析
    public StudentInfo loginStudent(@RequestBody Student student){
        String studentpass = student.getStudentpass();
        Integer studentid = student.getStudentid();
        studentpass = MD5utils.getMd5(studentpass, "UTF-8", false, 32);
        return studentservice.loginStudent(studentid,studentpass);
    }


    @RequestMapping("/loginAdmin.do")
    @ResponseBody
    public Admin loginAdmin(@RequestBody Admin admin){
        admin.setAdminpass(MD5utils.getMd5(admin.getAdminpass(), "UTF-8", false, 32));
        System.out.println(admin);
        Admin admin_temp =  adminService.adminLogin(admin);
        System.out.println(admin_temp);
        return admin_temp;
    }

    @RequestMapping("/showClass.do")
    @ResponseBody
    public ClassInfo showClass(@RequestBody com.reiuy.entity.Class c){
        return classService.showClass(c);
    }


    @RequestMapping("/showAllTeachers.do")
    @ResponseBody
    public JSONArray showTeachers(){
        return JSONArray.parseArray(JSON.toJSONString(teacherService.showAllTeachers()));
    }

    @RequestMapping("/showAllStudents.do")
    @ResponseBody
    public JSONArray showStudents(){
        return JSONArray.parseArray(JSON.toJSONString(studentservice.foundAllStudents()));
    }

    @RequestMapping("/showAllClasses.do")
    @ResponseBody
    public JSONArray showClasses(){
        return JSONArray.parseArray(JSON.toJSONString(classService.showAllClass()));
    }

    @RequestMapping("/showAllCourses.do")
    @ResponseBody
    public JSONArray showCourses(){
        return JSONArray.parseArray(JSON.toJSONString(courseService.showAllCourse()));
    }
}
